/**
 * Copyright(c) 2013 Jade Techonologies Co., Ltd.
 *
 * History:
 *   13-4-14 下午11:38 Created by dwzhang
 */
package com.jade.framework.base.pattern.pipeline;

import java.util.List;

/**
 * 基本实现的阀门处理链.
 *
 * @author dwzhang
 * @version 1.0 13-4-14 下午11:38
 */
public class SimpleValveChain<Context, E extends Exception> implements ValveChain<Context, E>
{
    protected List<Valve<Context, E>> valves = null;

    protected int index = 0;

    protected int size;

    public SimpleValveChain(List<Valve<Context, E>> valves)
    {
        this.valves = valves;
        this.size = valves.size();
    }

    public SimpleValveChain(List<Valve<Context, E>> valves, int index)
    {
        this(valves);
        this.index = index;
    }

    /**
     * 处理下一个阀门。
     *
     * @param context 处理上下文
     */
    public void handleNext(Context context)
            throws E
    {
        if (index < size) {
            Valve<Context, E> valve = valves.get(index++);
            valve.handle(context, this);
        }
    }
}

